package com.portal.gui.marketman;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Product;
import com.portal.logic.ProductDAO;

public class SelectProductToEditScr extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectProductToEditScr frame = new SelectProductToEditScr();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SelectProductToEditScr() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(12, 167, 97, 25);
		contentPane.add(btnBack);
		
		Connection connection = ConnectionManager.getConnection();
		final ProductDAO productDAO = new ProductDAO(connection) ;
		List<Product> products = productDAO.selectAll();

		final JComboBox cmbProducts = new JComboBox();
		Iterator<Product> iterator = products.iterator();
		while (iterator.hasNext()) {
			Product product = (Product) iterator.next();
			cmbProducts.addItem(product.getName());
		}
		
		
		cmbProducts.setBounds(179, 42, 116, 22);
		contentPane.add(cmbProducts);
		
		JLabel lblSelectProduct = new JLabel("Select Product:");
		lblSelectProduct.setBounds(73, 45, 94, 16);
		contentPane.add(lblSelectProduct);
		
		JButton btnStartEditProduct = new JButton("Start Edit Product");
		btnStartEditProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				String selectedProduct = (String)cmbProducts.getSelectedItem();
				EditProductScr editProductScr = new EditProductScr(selectedProduct);
				editProductScr.setVisible(true);
			}
		});
		btnStartEditProduct.setBounds(173, 108, 146, 25);
		contentPane.add(btnStartEditProduct);
	}

}
